package one.stand.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import one.stand.aop.ApiController;
import one.stand.entity.excel.CompanyExcelEntity;
import one.stand.entity.excel.OrdersExcelEntity;
import one.stand.enums.ResultEnum;
import one.stand.model.OrderinfoModel;
import one.stand.model.ResultModel;
import one.stand.request.OrdersRequest;
import one.stand.response.OrdersResponse;
import one.stand.util.ExcelUtil;
import one.stand.util.ExportDataDTO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.util.List;
import java.util.Map;

/**
 * 订单信息
 * @author
 */
@RestController
@RequestMapping("crmPc/orders")
@Slf4j
@Api(tags="订单信息")
public class OrdersController extends BaseController {

    @PostMapping("/selectOrdersInfoPc")
    @ApiOperation(value = "根据公司查询订单(订单状态（1待付款，2待发货，3待收货，4已完成，5交易取消，6退款成功）)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "createTimes", value = "下单时间", paramType = "String"),
            @ApiImplicitParam(name = "orderState", value = "订单状态（1待付款，2待发货，3待收货，4已完成，5交易取消，6退款成功）", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,company = true,card = true,paramClass =OrdersRequest.class )
    public ResultModel<Map<String, Object>> selectOrdersInfoPc(@RequestAttribute OrdersRequest request) {
        
        return crmFegin.selectOrdersInfoPc(request);
    }

    @PostMapping("/exportOrdersInfoPc")
    @ApiOperation(value = "导出公司查询订单(订单状态（1待付款，2待发货，3待收货，4已完成，5交易取消，6退款成功）)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "createTimes", value = "下单时间", paramType = "String"),
            @ApiImplicitParam(name = "orderState", value = "订单状态（1待付款，2待发货，3待收货，4已完成，5交易取消，6退款成功）", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,company = true,card = true,paramClass =OrdersRequest.class )
    public void exportOrdersInfoPc(@RequestAttribute OrdersRequest request, HttpServletResponse response) {
        ResultModel<ExportDataDTO<OrdersExcelEntity>> resultModel= crmFegin.exportOrdersInfoPc(request);
        if(resultModel.getCode().equals(ResultEnum.SUCCESS.getCode())){
            ExportDataDTO file = resultModel.getData();
            try{
                ExcelUtil.exportExcel(file, OrdersExcelEntity.class,response);
            }catch (Exception e){
                log.error("导出订单列表[异常]", e);
            }
        }
    }


    @PostMapping("/getOrdersInfo")
    @ApiOperation(value = "查询订单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ordersId", value = "订单id", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,company = true,card = true,paramClass =OrdersRequest.class)
    public ResultModel<List<OrdersResponse>> getOrdersInfo(@RequestAttribute OrdersRequest request) {
        
        return crmFegin.getOrdersInfo(request);
    }

    @PostMapping("/updateOrderState")
    @ApiOperation(value = "撤销订单，确认发货")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ordersId", value = "订单id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "orderState", value = "订单状态（1待付款，2待发货，3待收货，4已完成，5交易取消，6退款成功）", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,company = true,card = true,paramClass =OrdersRequest.class)
    public ResultModel updateOrderState(@RequestAttribute OrdersRequest request) {
        
        return crmFegin.updateOrderState(request);
    }

    @PostMapping("/updateRefundState")
    @ApiOperation(value = "修改订单退款状态1未申请，2退款申请中，3退款成功")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ordersId", value = "订单id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "refundState", value = "退款状态1未申请(拒绝)，2退款申请中，3退款成功", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,company = true,card = true,paramClass =OrdersRequest.class)
    public ResultModel updateRefundState(@RequestAttribute OrdersRequest request) {
        
        return crmFegin.updateRefundState(request);
    }

    @PostMapping("/delOrders")
    @ApiOperation(value = "删除已过期的订单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ordersId", value = "过期订单id", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,company = true,card = true,paramClass =OrdersRequest.class)
    public ResultModel delOrders(@RequestAttribute OrdersRequest request) {

        return crmFegin.delOrders(request);
    }
}
